package com.demo.service.mod.impl;

import com.demo.dao.mod.entity.*;
import com.demo.dao.mod.mapper.ModComponentMapper;
import com.demo.dao.mod.mapper.ModComponentPropsMapper;
import com.demo.dao.mod.mapper.ModPageMapper;
import com.demo.service.mod.ModPageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ModPageServiceImpl implements ModPageService {
    @Autowired
    private ModPageMapper pageMapper;

    @Autowired
    private ModComponentMapper componentMapper;

    @Autowired
    private ModComponentPropsMapper componentPropsMapper;


    //分页读取模板页面
    @Override
    public PageInfo<ModPageItem> findModPage(Map<String, String> params,
                                              Integer pageNum, Integer pageSize) {
        List<ModPageItem> lists;
        PageInfo<ModPageItem> pageInfo;

        // TODO Auto-generated method stub
        PageHelper.startPage(pageNum, pageSize);
        if (params.isEmpty()) {
            lists = pageMapper.selectAll();
            pageInfo = new PageInfo<ModPageItem>(lists);
        } else {
            lists = pageMapper.selectByParams(params);
            pageInfo = new PageInfo<ModPageItem>(lists);
        }

        return pageInfo;
    }

    @Override
    public boolean removeModPage(Long id) {
        Long comId = 0L;
        List<ModComponentItem> componentItemList = componentMapper.selectByPageId(id);
        for (int i=0; i<componentItemList.size(); i++) {
            comId = componentItemList.get(i).getId();

            //先删除props,data
            componentPropsMapper.deleteByComId(comId);

            //删除组件
            componentMapper.delete(comId);
        }

        //删除页面
        pageMapper.delete(id);
        return true;
    }
}
